#coding=utf-8
"""
===========================
Author:杭州多测师_xjp
Time:2023/3/24 14:39
website:https://duoceshi.net
===========================
"""
'''
此模块是封装所有用例的基类
比如所有用例要用到的元素定位方法，以及输入框的点击，下拉等公共方法

'''
import sys
sys.path.append('..')

import unittest
from time import *
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select


# 调试代码
# from selenium import webdriver
# drive=webdriver.Chrome()

class BasePage(unittest.TestCase):

    @classmethod
    def set_driver(cls,driver):  #入参是driver对象，把创建好的driver对象传进来，变成BasePage类的属性
        cls.driver=driver

    @classmethod
    def get_driver(cls):  #单例设计模式
        return cls.driver


    @classmethod  #封装元素定位
    def find_element(cls,elenment):
        type=elenment[0] #id
        value=elenment[1]  #kw
        if type=='id':
            elem=cls.driver.find_element_by_id(value)
        elif type=='class':
            elem=cls.driver.find_element_by_class_name(value)
        elif type=='xpath':
            elem=cls.driver.find_element_by_xpath(value)
        elif type=='partial':
            elem=cls.driver.find_element_by_partial_link_text(value)
        elif type == 'css':
            elem = cls.driver.find_element_by_css_selector(value)
        elif type=='link_text':
            elem=cls.driver.find_element_by_link_text(value)
        elif type=='name':
            elem=cls.driver.find_element_by_name(value)
        elif type=='js':
            elem=cls.driver.execute_script(value)


        else:  #如果不是上面的定位方式，则手动触发异常：请输入正确的参数
            raise ValueError('please input corrt paramters')

        return elem

    @classmethod  #封装输入函数
    def sendKeys(cls,elem,*text):
        return elem.send_keys(text)

    @classmethod  #封装点击操作
    def click(cls,elem):
        return elem.click()
    @classmethod   #封装隐式等待
    def wait(cls,sec):
        return cls.driver.implicitly_wait(sec)
    @classmethod
    def frame(cls,elem):
        return cls.driver.switch_to.frame(elem)  #切换iframe框

    @classmethod
    def outframe(cls):
        return cls.driver.switch_to.default_content()  #退出iframe框

    @classmethod  #封装显示等待
    def sleep(cls,sec):
        return sleep(sec)


    @classmethod
    def mouse(cls,element):  #模拟鼠标定位
        ActionChains(cls.driver).move_to_element(element).perform()
    @classmethod
    def close(cls):
        cls.driver.close()
    @classmethod
    def quit(cls):
        cls.driver.quit()
    @classmethod
    def get_text(cls,element):#获取标签文本信息
        value=BasePage.find_element(element).text
        return value
    @classmethod
    def get_title(cls):
        value=cls.driver.title  #获取窗口title信息
        return value
    @classmethod
    def open_new_window(cls,url):
        win='window.open(""+url+"")'
        cls.driver.execute_script(win)
    @classmethod
    def get_window_size(cls):
        size=cls.driver.get_window_size()
        return size
    @classmethod
    def back(cls):
        cls.driver.back()
    @classmethod
    def window_handles(cls):  #获取所有窗口的句柄信息，返回一个句柄列表
        handles=cls.driver.window_handles
        return handles
    @classmethod
    def select(cls,element,type,value):
        if type=='index':
            Select(element).select_by_index(value)
        elif type=='value':
            Select(element).select_by_value(value)
        elif type=='text':
            Select(element).deselect_by_visible_text(value)
    @classmethod
    def alert_accept(cls):
        cls.driver.switch_to.alert.accept()
    @classmethod
    def alert_dismiss(cls):
        cls.driver.switch_to.alert.dismiss()
    @classmethod
    def scroll(cls,scroll_range):
        js='window.scrollTo('+scroll_range+')'
        cls.driver.execute_script(js)




    @classmethod
    def get_text(cls,element):
        '''封装根据网页元素拿到text值'''
        value=BasePage.find_element(element).text
        return value

    @classmethod
    def refresh(cls):
        cls.driver.refresh()

if __name__ == '__main__':
    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get('http://www.baidu.com')
    driver.maximize_window()
    baidu_input=('id','kw')
    elem=BasePage.find_element(baidu_input)
    BasePage.sendKeys(elem,'多测师')
    baidu_click=('id','su')
    elem=BasePage.find_element(baidu_click)
    elem.click()




